/* $Id: Layout.java 138 2011-09-01 17:51:37Z altdotua@gmail.com $ */

package alt.djudge.frontend.client.ui.contest;

import alt.djudge.frontend.client.rpc.DJudgeGWTContestServiceAsync;
import alt.djudge.frontend.client.rpc.DJudgeGWTContestService;
import alt.djudge.frontend.client.rpc.DJudgeGWTLoginService;
import alt.djudge.frontend.client.rpc.DJudgeGWTLoginServiceAsync;
import alt.djudge.frontend.client.ui.AbstractLayout;
import alt.djudge.frontend.client.ui.ContentPage;
import alt.djudge.frontend.shared.dto.contest.UserLoginInfo;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ScrollPanel;


public class Layout extends AbstractLayout
{
	public static DJudgeGWTContestServiceAsync contestService = GWT.create(DJudgeGWTContestService.class);
	
	public static DJudgeGWTLoginServiceAsync loginService = GWT.create(DJudgeGWTLoginService.class);
	
	interface MyUiBinder extends UiBinder<DockLayoutPanel, Layout> {}

	private static MyUiBinder uiBinder = GWT.create(MyUiBinder.class);
	
	private String sessionId;
	
	private Long contestId = 3L;
	
	@UiField
	public ScrollPanel contentPanel;

	ContentPage[] pages;

	@UiField
	public FlowPanel westPanel;

	@UiField
	public FlowPanel flowPanel;

	@UiField
	Hyperlink hlLogin;
	
	@UiField
	Hyperlink hlRegister;
	
	@UiField
	HorizontalPanel panelLoggedIn;
	
	@UiField
	HorizontalPanel panelNotLoggedIn;
	
	@UiField
	Label lblUsername;
	
	static Layout layout;
	
	UserLoginInfo info;
	
	public static Layout getLayout()
	{
		return layout;
	}
	
	public Layout()
	{
		initWidget(uiBinder.createAndBindUi(this));
		layout = this;

		pages = new ContentPage[flowPanel.getWidgetCount()];
		for (int i = 0; i < pages.length; i++)
			pages[i] = (ContentPage) flowPanel.getWidget(i);
		
		onHistoryEvent(History.getToken());
	}
	
	public void onHistoryEvent(String historyToken)
	{
		if (historyToken.equals("login"))
		{
			showLoginForm();
			return;
		}
		if (historyToken.equals("register"))
		{
			showRegisterForm();
			return;
		}
		
		ContentPage[] pages = getPages();
		for (ContentPage page : pages)
		{
			page.setVisible(false);
		}
		
		if (historyToken.startsWith("contest-"))
		{
			String[] tokenSplit = historyToken.split("/", 2);
			contestId = Long.parseLong(tokenSplit[0].replaceAll("[\\D]", ""));
			historyToken = tokenSplit.length == 2 ? tokenSplit[1] : "";
			
		  	westPanel.clear();
		  	westPanel.add(new Hyperlink("Home", "contest-" + contestId + "/home"));
		  	westPanel.add(new Hyperlink("Problems", "contest-" + contestId + "/problems"));
		  	westPanel.add(new Hyperlink("Submit", "contest-" + contestId + "/submit"));
		  	westPanel.add(new Hyperlink("Submissions", "contest-" + contestId + "/submissions"));
		  	westPanel.add(new Hyperlink("Monitor", "contest-" + contestId + "/monitor"));
		}

		int cnt = 0;
		for (ContentPage page : pages)
		{
			cnt++;
			String pageToken = page.getHistoryTokenPrefix();
			if (cnt == pages.length || (historyToken.startsWith(pageToken) && (historyToken.equals(pageToken) || historyToken.charAt(pageToken.length()) == '/' || historyToken.charAt(pageToken.length()) == '-')))
			{
				page.setVisible(true);
				page.dispatch(historyToken);
				return;
			}
		}
	}
	
	@Override
	protected ContentPage[] getPages()
	{
		return pages;
	}

	public Long getContestId()
	{
		return contestId;
	}
	
	@Override
	protected void setSessionId(String sessionId)
	{
		this.sessionId = sessionId;
	}

	@Override
	protected String getSessionId()
	{
		return sessionId;
	}

	@Override
	public void setLoginInfo(UserLoginInfo info)
	{
		this.info = info;
		if (info != null)
		{
			panelNotLoggedIn.setVisible(false);
			panelLoggedIn.setVisible(true);
			lblUsername.setText(info.getUsername());
		}
	}
}
